<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once '/application/libraries/PHPExcel.php';

class Device extends CI_Controller {

    //构造函数
    public function __construct()
    {
		parent::__construct();
		$this->load->model('Config_m');
	    $this->load->model('Agent_m');
	}
	//添加设备
	public function adds(){
    	$device_sn = $_POST['device_sn'];
/*    	$device_sn = '868658039075912,868658039075911,868658039075913';*/
    	//设备号
        $data = explode(',', $device_sn);
        $success = array();
        $fail = array();
        foreach ($data as $key => $value) {
        	$sql = "select device_sn from mifi_device where device_sn = {$value}";
        	$device = $this->db->query($sql)->row_array();
        	//不存在设备进行添加
        	if(!$device){
        		$agents['addtime'] = date('Y-m-d H:i:s');
				$agents['device_sn'] = $value;
				$res = $this->db->insert('device', $agents);
        		$success[] = $value;
        	}
            
            //存在设备
        	if($device){
        		$fail[] = $value;
        	}
        }
        //拼接成字符串
        if(is_array($success)){
           $success = join(',',$success);
        }
        if(is_array($fail)){ 	
           $fail = join(',',$fail);
        }
       	
    	echo json_encode(['code'=>1,'infomsg'=>'添加成功','successs'=>$success,'fails'=>$fail]);
    }

	/*设备列表*/
	public function devicelists()
	{
		if(isset($_POST['key'])){
			$key = trim($_POST['key']);
			$cons = "where state=1 and device_sn like '%{$key}%'";
			$this->db->like('device_sn', $key);
		}else{
			$cons = "where state=1";
		}
		$this->db->where('state', 1);
	    $total = $this->db->count_all_results('device');
		$page = $_POST['page'];
        $limit = $_POST['limit'];
        $offset = ($page - 1) * $limit;
        $sql = "select * from  mifi_device $cons order by device_id desc limit ".$offset.",".$limit."";
        $device = $this->db->query($sql)->result_array();
       	if($device){
	        foreach ($device as $key => $val) {
				$a1['id'] = $val['agent1'];
				$agent1 = $this->Agent_m->get_one($a1);
				$a2['id'] = $val['agent2'];
				$agent2 = $this->Agent_m->get_one($a2);
              
				if (!empty($agent1)) {
					$device[$key]['agent1'] = $agent1['agent_name'];
				}else{
					$device[$key]['agent1'] = '';
				}

				if (!empty($agent2)) {
					$device[$key]['agent2'] = $agent2['agent_name'];
				}else{
					$device[$key]['agent2'] = '';
				}
				$device[$key]['state'] = '使用中';

			}
       	}
	    echo json_encode(['code'=>0,'count'=>$total,'data'=>$device]);
	}
	/*已停用的设备列表*/
	public function devicelists_one()
	{
		if(isset($_POST['key'])){
			$key = trim($_POST['key']);
			$cons = "where state=0 and device_sn like '%{$key}%'";
			$this->db->like('device_sn', $key);
		}else{
			$cons = "where state=0";
		}
		$this->db->where('state', 0);
	    $total = $this->db->count_all_results('device');
		$page = $_POST['page'];
        $limit = $_POST['limit'];
        $offset = ($page - 1) * $limit;
        $sql = "select * from  mifi_device $cons order by device_id desc limit ".$offset.",".$limit."";
        $device = $this->db->query($sql)->result_array();
       	if($device){
	        foreach ($device as $key => $val) {
				$a1['id'] = $val['agent1'];
				$agent1 = $this->Agent_m->get_one($a1);
				$a2['id'] = $val['agent2'];
				$agent2 = $this->Agent_m->get_one($a2);
              
				if (!empty($agent1)) {
					$device[$key]['agent1'] = $agent1['agent_name'];
				}else{
					$device[$key]['agent1'] = '';
				}

				if (!empty($agent2)) {
					$device[$key]['agent2'] = $agent2['agent_name'];
				}else{
					$device[$key]['agent2'] = '';
				}
				$device[$key]['state'] = '已停用';
			}
       	}
	    echo json_encode(['code'=>0,'count'=>$total,'data'=>$device]);
	}


	//一级代理
	public function agent1()
	{
        $sql = "select id,agent_name from mifi_agent where leader = 0 order by id desc";
		$agent1 = $this->db->query($sql)->result_array();
        echo json_encode(['code'=>1,'data'=>$agent1]);die;
	}

	//二级代理
	public function agent2()
	{
		$id = $this->input->post('id',true);
		$sql = "select id,agent_name from mifi_agent where agent_level = 2 order by id desc";
		$agent2 = $this->db->query($sql)->result_array();
		echo json_encode(['code'=>1,'datas'=>$agent2]);die;
	}

	//单条记录删除设备列表
	public function deldevice()
	{
		$device_id = $_POST['device_id'];
		$this->db->where('device_id', $device_id);
        $del = $this->db->delete('device'); 
        if($del){
           echo json_encode(['code'=>1,'infomsg'=>"删除成功"]);
        }else{
           echo json_encode(['code'=>0,'infomsg'=>"删除失败"]);
        }
	}

	//批量删除设备记录
	public function delalldevice(){
		$device_id = $_POST['device_id'];
		$this->db->where_in('device_id',$device_id);
        $delall = $this->db->delete('device');
        if($delall){ 
           echo json_encode(['code'=>1,'infomsg'=>"删除成功"]);	
        }else{
           echo json_encode(['code'=>0,'infomsg'=>"删除失败"]);
        }
	}

	//编辑代理
	public function editagent(){
		$agent1['agent1'] = json_decode($_POST['agent1']);
		$agent2['agent2'] = json_decode($_POST['agent2']);
		$yongjin1['yongjin1'] = json_decode($_POST['yongjin1']);
		if (!empty($agent2['agent2'])) {
			$yongjin2['yongjin2'] = json_decode($_POST['yongjin2']);
		}
		$ids = $_POST['ids'];
		if(!empty($agent2['agent2'])){
            foreach ($ids as $key => $val) {
			    $where['device_id'] = $val;
		   	    $this->db->where($where)->update('device',$agent1);
		   	    $this->db->where($where)->update('device',$agent2);
		   	    $this->db->where($where)->update('device',$yongjin1);
		   	    $this->db->where($where)->update('device',$yongjin2);
		    }
		}else{
			foreach ($ids as $key => $val) {
			    $where['device_id'] = $val;
		   	    $this->db->where($where)->update('device',$agent1);
		   	    $this->db->where($where)->update('device',$yongjin1);
		    }
		}
		echo json_encode(['code'=>1,'infomsg'=>"代理修改成功"]);	
	}
}
